package com.skydroid.rcsdkdemo;

import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;

/**
 * UDP消息测试活动
 * 用于测试向特定IP和端口发送UDP消息
 */
public class UDPMessageTestActivity extends AppCompatActivity {

    private static final String DEFAULT_SERVER_ADDRESS = "192.168.144.104";
    private static final int DEFAULT_SERVER_PORT = 8888;
    private static final String DEFAULT_MESSAGE = "Test message";

    private EditText etServerAddress;
    private EditText etServerPort;
    private EditText etMessage;
    private Button btnSend;
    private TextView tvResponse;
    
    private UDPMessageSender messageSender;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_udp_message_test);
        
        // 初始化视图
        initViews();
        
        // 初始化UDP消息发送器
        initMessageSender();
        
        // 设置按钮点击事件
        setListeners();
    }

    private void initViews() {
        etServerAddress = findViewById(R.id.et_server_address);
        etServerPort = findViewById(R.id.et_server_port);
        etMessage = findViewById(R.id.et_message);
        btnSend = findViewById(R.id.btn_send);
        tvResponse = findViewById(R.id.tv_response);
        
        // 设置默认值
        etServerAddress.setText(DEFAULT_SERVER_ADDRESS);
        etServerPort.setText(String.valueOf(DEFAULT_SERVER_PORT));
        etMessage.setText(DEFAULT_MESSAGE);
    }

    private void initMessageSender() {
        messageSender = new UDPMessageSender(DEFAULT_SERVER_ADDRESS, DEFAULT_SERVER_PORT);
        messageSender.setOnMessageReceivedListener(new UDPMessageSender.OnMessageReceivedListener() {
            @Override
            public void onMessageReceived(String message) {
                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        tvResponse.setText("收到响应: " + message);
                    }
                });
            }
        });
    }

    private void setListeners() {
        btnSend.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                sendMessage();
            }
        });
    }

    private void sendMessage() {
        String address = etServerAddress.getText().toString().trim();
        String portStr = etServerPort.getText().toString().trim();
        String message = etMessage.getText().toString().trim();
        
        // 检查输入是否有效
        if (address.isEmpty() || portStr.isEmpty() || message.isEmpty()) {
            tvResponse.setText("请输入有效的服务器地址、端口和消息");
            return;
        }
        
        int port;
        try {
            port = Integer.parseInt(portStr);
        } catch (NumberFormatException e) {
            tvResponse.setText("请输入有效的端口号");
            return;
        }
        
        // 如果地址或端口发生变化，重新创建UDP发送器
        if (!address.equals(messageSender.getServerAddress()) || port != messageSender.getServerPort()) {
            messageSender.close();
            messageSender = new UDPMessageSender(address, port);
            messageSender.setOnMessageReceivedListener(new UDPMessageSender.OnMessageReceivedListener() {
                @Override
                public void onMessageReceived(String response) {
                    runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            tvResponse.setText("收到响应: " + response);
                        }
                    });
                }
            });
        }
        
        // 发送消息
        boolean success = messageSender.sendMessage(message);
        if (success) {
            tvResponse.setText("已发送消息: " + message);
        } else {
            tvResponse.setText("发送消息失败");
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (messageSender != null) {
            messageSender.close();
        }
    }
} 